﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// CeLiangYiView.xaml 的交互逻辑
    /// </summary>
    public partial class CeLiangYiView : UserControl
    {
        ViewModels.CeLiangYiViewModel viewModel = new ViewModels.CeLiangYiViewModel();
        public CeLiangYiView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            ///根据仪器型号初始化数据
            switch (model)
            {
                case DeviceEnum.DeviceModelEnum.AT512: viewModel.HasR = true;viewModel.MenuIndex = 2; break;
                case DeviceEnum.DeviceModelEnum.DM3058E: viewModel.HasVol = true; viewModel.HasCur = true; viewModel.HasR = true; viewModel.HasDuanLu = true;viewModel.HasErJiGuan = true; break;
                case DeviceEnum.DeviceModelEnum.UTR2830E: viewModel.HasDianRong = true; viewModel.HasDianGan = true; viewModel.MenuIndex = 4; break;
                default:
                    break;
            }
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令更新界面信息
                Enum.TryParse(order.temp, out CeLiangYiEnum.TestMode mode);
                CeLiangYi parameter = JsonConvert.DeserializeObject<CeLiangYi>(order.deviceModel);
                switch (mode)
                {
                    case CeLiangYiEnum.TestMode.测量电压:
                        viewModel.MenuIndex = 0;
                        viewModel.VolChannel = parameter.通道;
                        viewModel.VolTemp = parameter.测量类型;
                        viewModel.VolMin = parameter.下限值?.ToString();
                        viewModel.VolMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.测量电流:
                        viewModel.MenuIndex = 1;
                        viewModel.CurChannel = parameter.通道;
                        viewModel.CurTemp = parameter.测量类型;
                        viewModel.CurMin = parameter.下限值?.ToString();
                        viewModel.CurMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.测量电阻:
                        viewModel.MenuIndex = 2;
                        viewModel.RChannel = parameter.通道;
                        viewModel.RTemp = parameter.测量类型;
                        viewModel.RMin = parameter.下限值?.ToString();
                        viewModel.RMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.短路测试:
                        viewModel.MenuIndex = 3;
                        viewModel.DuanLuChannel = parameter.通道;
                        viewModel.DuanLuMin = parameter.下限值?.ToString();
                        viewModel.DuanLuMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.二极管测试:
                        viewModel.MenuIndex = 4;
                        viewModel.ErJiGuanChannel = parameter.通道;
                        viewModel.ErJiGuanMin = parameter.下限值?.ToString();
                        viewModel.ErJiGuanMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.测量电容:
                        viewModel.MenuIndex = 5;
                        viewModel.DianRongChannel = parameter.通道;
                        viewModel.DianRongTemp = parameter.测量类型;
                        viewModel.DianRongVol = parameter.测量电平.ToString();
                        viewModel.DianRongMin = parameter.下限值?.ToString();
                        viewModel.DianRongMax = parameter.上限值?.ToString();
                        break;
                    case CeLiangYiEnum.TestMode.测量电感:
                        viewModel.MenuIndex = 6;
                        viewModel.DianGanChannel = parameter.通道;
                        viewModel.DianGanTemp = parameter.测量类型;
                        viewModel.DianGanVol = parameter.测量电平.ToString();
                        viewModel.DianGanMin = parameter.下限值?.ToString();
                        viewModel.DianGanMax = parameter.上限值?.ToString();
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public object Device;

        private void VolClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.VolMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.VolMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.测量电压,
                测量类型 = viewModel.VolTemp,
                通道 = viewModel.VolChannel,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void CurClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.CurMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.CurMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.测量电流,
                测量类型 = viewModel.CurTemp,
                通道 = viewModel.CurChannel,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void RClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.RMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.RMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.测量电阻,
                测量类型 = viewModel.RTemp,
                通道 = viewModel.RChannel,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void DuanLuClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.DuanLuMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DuanLuMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.短路测试,
                通道 = viewModel.DuanLuChannel,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void DianRongClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null; double? vol = 0;
            if (!DataConvert.GetDouble(viewModel.DianRongVol, ref vol))
            {
                View.Controls.Dialogs.TipView.Show("电平输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DianRongMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DianRongMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.测量电容,
                测量类型 = viewModel.DianRongTemp,
                通道 = viewModel.DianRongChannel,
                测量电平 = vol ?? 0.0,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void DianGanClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null; double? vol = 0;
            if (!DataConvert.GetDouble(viewModel.DianGanVol, ref vol))
            {
                View.Controls.Dialogs.TipView.Show("电平输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DianGanMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DianGanMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.测量电感,
                测量类型 = viewModel.DianGanTemp,
                通道 = viewModel.DianGanChannel,
                测量电平 = vol ?? 0.0,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }

        private void ErJiGuanClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.ErJiGuanMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.ErJiGuanMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            CeLiangYi parameter = new CeLiangYi()
            {
                mode = CeLiangYiEnum.TestMode.二极管测试,
                通道 = viewModel.ErJiGuanChannel,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder((Device as IDevice).Name));
            }
        }
    }
}
